import 'package:citiesocial/_include.dart';
import 'package:citiesocial/ui/main/catalogue/catalogue_next.dart';

///首页-目录
class Catalogue extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _CatalogueState();
}

class _CatalogueState extends BaseState<Catalogue> {
  final _topList = List<ThemeEntity>();
  final _catalogueList = List<CategoryEntity>();

  void _loadData() async {
    LoadingDialog.show(context);
    List<ThemeEntity> theme =
        await HttpManager.themes(context: context, pagesize: 4);
    if (theme != null && theme.length > 0) {
      _topList.clear();
      setState(() {
        _topList.addAll(theme);
      });
    }
    _loadCatalogue();
  }

  void _loadCatalogue() async {
    List<CategoryEntity> list =
        await HttpManager.directoryCategory(context: context);
    if (list != null && list.length > 0) {
      _catalogueList.clear();
      setState(() {
        _catalogueList.addAll(list);
      });
    }
    LoadingDialog.dismiss(context);
  }

  void _onItemCliek(int index) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) => CatalogueNext('${_catalogueList[index].cat_id}',
            '${_catalogueList[index].cat_name}'),
      ),
    );
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final widgets = List<Widget>();
    if (_topList.length > 0) {
      widgets.add(ThemeCatalogue(_topList));
      // widgets.add(Text('${_topList.length}'));
    }
    if (_catalogueList.length > 0) {
      widgets.addAll(List.generate(
        _catalogueList.length,
        (index) => ItemMenu(
              '${_catalogueList[index].cat_name}',
              rightIcon: 'imgs/js_cells_img_more_g.png',
              onTap: () => _onItemCliek(index),
            ),
      ));
    }
    return DListView(
      itemCount: widgets.length,
      itemBuilder: (_, index) => widgets[index],
      onRefresh: () async => _loadData(),
    );
  }
}
